package com.kotlin.kotlindemo

import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.widget.Button
import android.widget.Toast
import com.orhanobut.logger.Logger
import kotlinx.android.synthetic.main.activity_main2.*
import java.util.*

class Main2Activity : AppCompatActivity() {
    private var btn_test: Button? = null
    private var forecastList: RecyclerView? = null
    private val items = listOf(
            "Mon 6/23 - Sunny - 31/17",
            "Tue 6/24 - Foggy - 21/8",
            "Wed 6/25 - Cloudy - 22/17",
            "Thurs 6/26 - Rainy - 18/11",
            "Fri 6/27 - Foggy - 21/10",
            "Sat 6/28 - TRAPPED IN WEATHERSTATION - 23/18",
            "Sun 6/29 - Sunny - 20/7"
    )

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main2)
        btn_test = findViewById(R.id.btn_test) as Button?
        btn_test!!.setOnClickListener {
            toast("Click")
            thread("http://www.weather.com.cn/data/sk/101010100.html")
        }

        btn_test2.setOnClickListener {
            Logger.d("debug${Random().nextInt(100)}")
            btn_test2.text = "text${Random().nextInt(100)}"
        }

        forecastList = findViewById(R.id.forecast_list) as RecyclerView
        forecastList!!.layoutManager = LinearLayoutManager(this)
        forecastList!!.adapter = ForecastListAdapter(items)


    }

    private fun toast(s: String) {
        Toast.makeText(this, "fun_" + s, Toast.LENGTH_SHORT).show()
    }

    /**
     * @param url 请求网址
     * **/
    fun thread(url: String): Thread {
        val thread = object : Thread() {
            override fun run() {
                super.run()
                Request(url).run()
            }
        }
        thread.start()
        return thread
    }
}
